import React, { Component } from 'react'

const withPosition = WrappedComponent => {
  return class extends Component {
    state = {
      position: {
        x: 0,
        y: 0
      }
    }

    move = e => {
      this.setState({
        position: {
          x: e.clientX,
          y: e.clientY
        }
      })
    }

    componentDidMount () {
      console.log('111')
      window.addEventListener('mousemove', this.move)
    }
    componentWillUnmount () {
      window.removeEventListener('mousemove', this.move)
    }

    render () {
      return (
        <div>
          <WrappedComponent position={this.state.position} />
        </div>
      )
    }
  }
}

export { withPosition }
